package com.jikeh.service.impl;

import com.jikeh.mapper.UserMapper;
import com.jikeh.model.User;
import com.jikeh.service.UserService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jikeh
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> findByUser(User user, Integer offset, Integer limit) {
        RowBounds rowBounds = RowBounds.DEFAULT;
        if(offset != null && limit != null){
            rowBounds = new RowBounds(offset, limit);
        }
        return userMapper.selectByUser(user, rowBounds);
    }

    @Override
    public boolean saveOrUpdate(User user) {
        if(user.getId() == null){
            return userMapper.insert(user) == 1;
        } else {
            return userMapper.updateById(user) == 1;
        }
    }

    @Override
    public boolean deleteById(Long id) {
    	if(id == null){
    		throw new NullPointerException("id");
    	}
        return userMapper.deleteById(id) == 1;
    }
}
